import collections
# class Solution(object):
#     def canConvert(self, str1, str2):
#         """
#         :type str1: str
#         :type str2: str
#         :rtype: bool
#         """
#         d = {}
#         n = len(str1)
#         c = collections.defaultdict(int)
#         for i in range(n):
#             if str1[i] not in d:
#                 d[str1[i]] = str2[i]
#                 c[str2[i]] += 1
#                 if str1[i] not in c:
#                     c[str1[i]] = 0
#             else:
#                 if d[str1[i]] != str2[i]:
#                     return False
#         visited = set()
#         now = set()
#         for key in c:
#             if c[key] == 0:
#                 visited.add(key)
#                 now.add(key)
#             else:
#                 if key in d and d[key] == key:
#                     c[key] -= 1
#                     visited.add(key)
#         while now:
#             next_set = set()
#             for key in now:
#                 c[d[key]] -= 1
#                 if c[d[key]] == 0:
#                     visited.add(d[key])
#                     if d[key] in d:
#                         next_set.add(d[key])
#             now = next_set
#         if len(visited) == len(c):
#             return True
#         else:
#             if len(c) < 26:
#                 return True
#             else:
#                 to_set = set()
#                 for key in d:
#                     to_set.add(d[key])
#                 if len(to_set) < 26:
#                     return True
#                 else:
#                     return False

class Solution(object):
    def canConvert(self, str1, str2):
        """
        :type str1: str
        :type str2: str
        :rtype: bool
        """
        n = len(str1)
        d = {}
        to_set = set()
        for i in range(n):
            to_set.add(str2[i])
            if str1[i] not in d:
                d[str1[i]] = str2[i]
            else:
                if d[str1[i]] != str2[i]:
                    return False
        if len(to_set) == 26:
            return str1 == str2
        else:
            return True


data = Solution()
str1 = "aabcc"
str2 = "ccddd"
print(data.canConvert(str1, str2))
data = Solution()
str1 = "ab"
str2 = "ba"
print(data.canConvert(str1, str2))
str1 = "abcdefghijklmnopqrstuvwxyz"
str2 = "bcdefghijklmnopqrstuvwxyza"
print(data.canConvert(str1, str2))
str1 = "abcdefghijklmnopqrstuvwxyz"
str2 = "bcadefghijklmnopqrstuvwxzz"
print(data.canConvert(str1, str2))
str1 = "abcdefghijklmnopqrstuvwxyz"
str2 = "abcdefghijklmnopqrstuvwxyz"
print(data.canConvert(str1, str2))

